/**
 * Grammar for DAAS QL
 */
grammar Daasql;
options {
    output = AST;
    k = 1;
}
/*
tokens {
    AND;
    OR;
    EQ;
    GT;
    GE;
    LT;
    LE;
    LIKE;
}
*/
@header {
    package com.deepclassifieds.daas.ql;
}

@lexer::header {
    package com.deepclassifieds.daas.ql;
}

/* Bombing on first error now.  TODO: collect errors, recover... */
@members {
    protected void mismatch( IntStream input, int tokenType, BitSet follow ) throws RecognitionException {
        throw new MismatchedTokenException( tokenType, input );
    }
    
    public void recoverFromMismatchedSet( IntStream input, RecognitionException ex, BitSet follow )
        throws RecognitionException {
        
        throw ex;
    }
}

@rulecatch {
    catch ( RecognitionException ex ) {
        throw ex;
    }
}

string 	: 	SINGLE_QUOTED_STRING
	|	DOUBLE_QUOTED_STRING
    	;/* A.8, rule 67 */
    
/* name	:	VARNAME
	; */
	
name 	: ( PNAME_CHARS_PLUS_UNDERSCORE | '0' .. '9' )
    	( PNAME_CHARS_PLUS_UNDERSCORE
      	| '0' .. '9'
      	)*
      	;	
	   
value 	:	string
	|	number
	;    

number 	:	INTEGER
	|	DECIMAL
	|	DOUBLE
	;

where 	:	expr
	;

expr 	: 	exprOR
	|	exprAND
	|	exprEQ
	|	exprNE
	|	exprGT
	|	exprGE
	|	exprLT
	|	exprLE
	|	exprLIKE
    	;

exprOR 	:   OR LPAR expr COMMA expr RPAR 
    	;
/* A.8, rule 48 */
exprAND :  AND LPAR expr COMMA expr RPAR
    	;

exprEQ 	:   EQ LPAR name COMMA value RPAR
	;

exprNE 	:   NE LPAR name COMMA value RPAR
	;

exprGT 	:   GT LPAR name COMMA value RPAR
	;

exprGE  :   GE LPAR name COMMA value RPAR
	;

exprLT 	:   LT LPAR name COMMA value RPAR
	;
/* A.8, rule 49 */
exprLE 	:   LE LPAR name COMMA value RPAR
	;

exprLIKE : LIKE LPAR name COMMA string RPAR
	;
	
orderBy :	orderByASC 
	|	orderByDESC 
    	;

/* A.8, rule 17 */
orderByASC : ASC '(' name ')'
	;

orderByDESC : DESC '(' name ')'
	;
	
OR 	:	'or'   ;
AND 	:  	'and'  ;
EQ 	:	'eq'   ;
NE 	:	'ne'   ;
GT	:	'gt'   ;
GE	:	'ge'   ;
LT	:	'lt'   ;/* A.8, rule 49 */
LE	:	'le'   ;
LIKE	: 	'like' ;
ASC	:	'asc'  ;
DESC	:	'desc' ;

LPAR    :	'(' ;
RPAR	:	')' ;
COMMA	:	',' ;
/* A.8, rule 45 */
IRI_REFERENCE : '<' ~( '<' | '>' | '"' | '{' | '}' | '|' | '^' | '`' | '\\' | '\u0000' .. '\u0020' )* '>'
    ;

/* A.8, rule 71 */
/* PNAME_NAMESPACE : PNAME_PREFIX? ':'
    ; */

/* A.8, rule 72 */
/* PNAME_LOCAL_NAME : PNAME_NAMESPACE PNAME_LOCAL
    ; */

/* A.8, rule 73 */
/* BLANK_NODE_LABEL : '_:' PNAME_LOCAL
    ; */

/* A.8, rule 74 */
/* QUESTION_VARNAME : '?' VARNAME
    ; */

/* A.8, rule 75 */
/* DOLLAR_VARNAME : '$' VARNAME
    ; */

/* A.8, rule 76 */
/* LANGUAGE_TAG : '@' ( 'a' .. 'z' | 'A' .. 'Z' )+ ( '-' ( 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9' )+ )*
    ; */

/* A.8, rule 77 */
INTEGER : ( '0' .. '9' )+
    ;

/* A.8, rule 78 */
DECIMAL : ( '0' .. '9' )+ '.' ( '0' .. '9' )*
    | '.' ( '0' .. '9' )+
    ;

/* A.8, rule 79 */
DOUBLE : ( '0' .. '9' )+ '.' ( '0' .. '9' )* EXPONENT
    | '.' ( '0' .. '9' )+ EXPONENT
    | ( '0' .. '9' )+ EXPONENT
    ;

/* A.8, rule 80 */
POSITIVE_INTEGER : '+' INTEGER
    ;

/* A.8, rule 81 */
POSITIVE_DECIMAL : '+' DECIMAL
    ;

/* A.8, rule 82 */
POSITIVE_DOUBLE : '+' DOUBLE
    ;

/* A.8, rule 83 */
NEGATIVE_INTEGER : '-' INTEGER
    ;

/* A.8, rule 84 */
NEGATIVE_DECIMAL : '-' DECIMAL
    ;

/* A.8, rule 85 */
NEGATIVE_DOUBLE : '-' DOUBLE
    ;

/* A.8, rule 86 */
EXPONENT : ( 'e' | 'E' ) ( '+' | '-' )? ( '0' .. '9' )+
    ;

/* A.8, rule 89 */
/* LONG_SINGLE_QUOTED_STRING : '\'\'\'' ( ( '\'' | '\'\'' )? ( ~( '\'' | '\\' ) | ESCAPE_CHARACTER ) )* '\'\'\''
    ; */	

/* A.8, rule 90 */
/* LONG_DOUBLE_QUOTED_STRING : '"""' ( ( '"' | '""' )? ( ~( '"' | '\\' ) | ESCAPE_CHARACTER ) )* '"""'
    ; */	

/* A.8. rule 87 */
SINGLE_QUOTED_STRING : '\'' ( ~( '\u0027' | '\u005C' | '\u000A' | '\u000D' ) | ESCAPE_CHARACTER )* '\''
    ;

/* A.8. rule 88 */
DOUBLE_QUOTED_STRING : '"' ( ~( '\u0022' | '\u005C' | '\u000A' | '\u000D' ) | ESCAPE_CHARACTER )* '"'
    ;

/* A.8, rule 91 */
ESCAPE_CHARACTER : '\\' ( 't' | 'b' | 'n' | 'r' | 'f' | '\\' | '"' | '\'' )
    ;

/* A.8, rule 92 */
/* NIL : '(' WHITESPACE* ')'
    ; */

/* A.8, rule 93 */
/* WHITESPACE : ( '\u0020' | '\u0009' | '\u000D' | '\u000A' ) { $channel = HIDDEN; }
    ; */

/* A.8, rule 94 */
/* ANONYMOUS : '[' WHITESPACE* ']'
    ; */

/* A.8, rule 97 */
/* VARNAME : ( PNAME_CHARS_PLUS_UNDERSCORE | '0' .. '9' )
    ( PNAME_CHARS_PLUS_UNDERSCORE
      | '0' .. '9'
      )*
      ; */

/* A.8, rule 99 */
/* PNAME_PREFIX : PNAME_CHARS_BASE ( ( PNAME_CHARS | '.' )* PNAME_CHARS )?
    ; */

/* A.8, rule 100 */
/* PNAME_LOCAL : ( PNAME_CHARS_PLUS_UNDERSCORE | '0' .. '9' ) ( ( PNAME_CHARS | '.' )* PNAME_CHARS )?
    ; */

/* A.8, rule 98 */
/* PNAME_CHARS : ( PNAME_CHARS_PLUS_UNDERSCORE
    | '-'
    | '0' .. '9'
    )
    ; */


/* A.8, rule 96 */
PNAME_CHARS_PLUS_UNDERSCORE : ( ( 'A' .. 'Z' | 'a' .. 'z' ) | '_' )
    ;

/* A.8, rule 95 */
/* PNAME_CHARS_BASE : ( 'A' .. 'Z'
    | 'a' .. 'z'
    )
    ; */
